﻿
// PBoxTestDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "PBoxTest.h"
#include "PBoxTestDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CPBoxTestDlg 对话框



CPBoxTestDlg::CPBoxTestDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PBOXTEST_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	_data = 0;
	pdata = 0;
	length = 0;
}

void CPBoxTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_combo_mode);
}

BEGIN_MESSAGE_MAP(CPBoxTestDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON3, &CPBoxTestDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CPBoxTestDlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, &CPBoxTestDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &CPBoxTestDlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON1, &CPBoxTestDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CPBoxTestDlg::OnBnClickedButton2)
END_MESSAGE_MAP()


// CPBoxTestDlg 消息处理程序

BOOL CPBoxTestDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	ULONGLONG temp[2];
	PList<char> key;
	if (getCmdSeed(key) < 0) MessageBox(TEXT("初始化SEED失败"));
	ULONGLONG t = GetTickCount64();
	key.append((char*)&theApp.m_hInstance, sizeof(HINSTANCE));
	key.append((char*)&this->m_hWnd, sizeof(HWND));	
	key.append((char*)&t, sizeof(ULONGLONG));
	key.append((char*)temp, sizeof(temp));

	rand_box.init((const byte *)key.begin(),key.count());

	m_combo_mode.SetCurSel(3);

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CPBoxTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CPBoxTestDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CPBoxTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CPBoxTestDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
}


void CPBoxTestDlg::OnBnClickedButton4()//load file
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog fDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("所有文件(*.*)|*.*||"));
	if (fDlg.DoModal() == IDOK)
	{
		CString st = fDlg.GetPathName();
		CFile file(st, CFile::modeRead);
		length= file.GetLength();
		if (_data) delete[] _data;
		_data = new unsigned char[length + pbox1024::groupSize()];
		pdata = _data + pbox1024::groupSize();
		file.Read(pdata, length);
		st.Format(TEXT("数据长度: %d"), length);
		SetDlgItemText(IDC_STATIC_LEN, st);
	}
}


void CPBoxTestDlg::OnBnClickedButton5()//save file
{
	CFileDialog fDlg(FALSE, NULL, NULL, OFN_HIDEREADONLY, _T("所有文件(*.*)|*.*||"));
	if (fDlg.DoModal() == IDOK)
	{
		CString st = fDlg.GetPathName();
		CFile file(st, CFile::modeCreate | CFile::modeWrite);
		file.Write(pdata, length);
		st.Format(TEXT("数据长度: %d"), length);
		SetDlgItemText(IDC_STATIC_LEN, st);
	}
}


void CPBoxTestDlg::OnBnClickedButton6()//hash
{
	int mode=m_combo_mode.GetCurSel();
	int len = 0;
	unsigned long long h[16] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };//hash初值为0
	DWORD t = GetTickCount();
	if (mode == 0)
	{
		len = pbox64::groupSize();
		pbox64::hash(h[0], pdata, length);
	}
	else if (mode == 1)
	{
		len = pbox128::groupSize();
		pbox128::hash(h, pdata, length);
	}
	else if (mode == 2)
	{
		len = pbox192::groupSize();
		pbox192::hash(h, pdata, length);
	}
	else if (mode == 3)
	{
		len = pbox256::groupSize();
		pbox256::hash(h, pdata, length);
	}
	else if (mode == 4)
	{
		len = pbox384::groupSize();
		pbox384::hash(h, pdata, length);
	}
	else if (mode == 5)
	{
		len = pbox512::groupSize();
		pbox512::hash(h, pdata, length);
	}
	else if (mode == 6)
	{
		len = pbox768::groupSize();
		pbox768::hash(h, pdata, length);
	}
	else if (mode == 7)
	{
		len = pbox1024::groupSize();
		pbox1024::hash(h, pdata, length);
	}
	t = GetTickCount() - t;
	unsigned char* p = (unsigned char*)h;
	CString str, s;
	for (int i = 0; i < len; ++i)
	{
		s.Format(TEXT("%02X"), p[i]);
		str += s;
	}
	SetDlgItemText(IDC_EDIT3, str);
	s.Format(TEXT("用时: %d ms"), t);
	SetDlgItemText(IDC_STATIC_TIME, s);
}


void CPBoxTestDlg::OnBnClickedButton1()//encrypt
{
	int mode = m_combo_mode.GetCurSel();
	int len = 0, flag = 0;
	CString k;
	GetDlgItemText(IDC_EDIT1, k);
	DWORD t = GetTickCount();
	if (mode == 0)
	{
		len = pbox64::groupSize();
		if (pdata - len < _data) flag = -1;
		else
		{
			unsigned long long* iv= (unsigned long long*) (pdata - len);
			iv[0] = rand_box.uint64();
			pbox64 p64;
			p64.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p64.setIV(iv[0]);
			p64.encrypt(pdata, length);
		}
	}
	else if (mode == 1)
	{
		len = pbox128::groupSize();
		if (pdata - len < _data) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) (pdata - len);
			for (int i = 0; i < pbox128::IVSize(); ++i) iv[i] = rand_box.uint64();
			pbox128 p128;
			p128.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p128.setIV(iv);
			p128.encrypt(pdata, length);
		}
	}
	else if (mode == 2)
	{
		len = pbox192::groupSize();
		if (pdata - len < _data) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) (pdata - len);
			for (int i = 0; i < pbox192::IVSize(); ++i) iv[i] = rand_box.uint64();
			pbox192 p192;
			p192.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p192.setIV(iv);
			p192.encrypt(pdata, length);
		}
	}
	else if (mode == 3)
	{
		len = pbox256::groupSize();
		if (pdata - len < _data) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) (pdata - len);
			for (int i = 0; i < pbox256::IVSize(); ++i) iv[i] = rand_box.uint64();
			pbox256 p256;
			p256.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p256.setIV(iv);
			p256.encrypt(pdata, length);
		}
	}
	else if (mode == 4)
	{
		len = pbox384::groupSize();
		if (pdata - len < _data) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) (pdata - len);
			for (int i = 0; i < pbox384::IVSize(); ++i) iv[i] = rand_box.uint64();
			pbox384 p384;
			p384.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p384.setIV(iv);
			p384.encrypt(pdata, length);
		}
	}
	else if (mode == 5)
	{
		len = pbox512::groupSize();
		if (pdata - len < _data) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) (pdata - len);
			for (int i = 0; i < pbox512::IVSize(); ++i) iv[i] = rand_box.uint64();
			pbox512 p512;
			p512.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p512.setIV(iv);
			p512.encrypt(pdata, length);
		}
	}
	else if (mode == 6)
	{
		len = pbox768::groupSize();
		if (pdata - len < _data) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) (pdata - len);
			for (int i = 0; i < pbox768::IVSize(); ++i) iv[i] = rand_box.uint64();
			pbox768 p768;
			p768.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p768.setIV(iv);
			p768.encrypt(pdata, length);
		}
	}
	else if (mode == 7)
	{
		len = pbox1024::groupSize();
		if (pdata - len < _data) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) (pdata - len);
			for (int i = 0; i < pbox1024::IVSize(); ++i) iv[i] = rand_box.uint64();
			pbox1024 p1024;
			p1024.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p1024.setIV(iv);
			p1024.encrypt(pdata, length);
		}
	}
	t = GetTickCount() - t;
	if (flag < 0) MessageBox(TEXT("IV缓存空间不足"));
	else
	{
		pdata -= len;
		length += len;
		CString str, s;
		for (int i = 0; i < len; ++i)
		{
			s.Format(TEXT("%02X"), pdata[i]);
			str += s;
		}
		SetDlgItemText(IDC_EDIT2, str);
		s.Format(TEXT("数据长度: %d"), length);
		SetDlgItemText(IDC_STATIC_LEN, s);
		s.Format(TEXT("用时: %d ms"), t);
		SetDlgItemText(IDC_STATIC_TIME, s);
	}
}


void CPBoxTestDlg::OnBnClickedButton2()//decrypt
{
	int mode = m_combo_mode.GetCurSel();
	int len = 0, flag = 0;
	CString k;
	GetDlgItemText(IDC_EDIT1, k);
	DWORD t = GetTickCount();
	if (mode == 0)
	{
		len = pbox64::groupSize();
		if (length < len) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) pdata;
			pbox64 p64;
			p64.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p64.setIV(iv[0]);
			p64.decrypt(pdata + len, length - len);
		}
	}
	else if (mode == 1)
	{
		len = pbox128::groupSize();
		if (length < len) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) pdata;
			pbox128 p128;
			p128.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p128.setIV(iv);
			p128.decrypt(pdata + len, length - len);
		}
	}
	else if (mode == 2)
	{
		len = pbox192::groupSize();
		if (length < len) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) pdata;
			pbox192 p192;
			p192.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p192.setIV(iv);
			p192.decrypt(pdata + len, length - len);
		}
	}
	else if (mode == 3)
	{
		len = pbox256::groupSize();
		if (length < len) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) pdata;
			pbox256 p256;
			p256.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p256.setIV(iv);
			p256.decrypt(pdata + len, length - len);
		}
	}
	else if (mode == 4)
	{
		len = pbox384::groupSize();
		if (length < len) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) pdata;
			pbox384 p384;
			p384.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p384.setIV(iv);
			p384.decrypt(pdata + len, length - len);
		}
	}
	else if (mode == 5)
	{
		len = pbox512::groupSize();
		if (length < len) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) pdata;
			pbox512 p512;
			p512.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p512.setIV(iv);
			p512.decrypt(pdata + len, length - len);
		}
	}
	else if (mode == 6)
	{
		len = pbox768::groupSize();
		if (length < len) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) pdata;
			pbox768 p768;
			p768.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p768.setIV(iv);
			p768.decrypt(pdata + len, length - len);
		}
	}
	else if (mode == 7)
	{
		len = pbox1024::groupSize();
		if (length < len) flag = -1;
		else
		{
			unsigned long long* iv = (unsigned long long*) pdata;
			pbox1024 p1024;
			p1024.setKey((const unsigned char*)k.GetString(), k.GetLength() * sizeof(TCHAR));
			p1024.setIV(iv);
			p1024.decrypt(pdata + len, length - len);
		}
	}
	t = GetTickCount() - t;
	if(flag < 0) MessageBox(TEXT("数据长度不足以解密"));
	else
	{
		CString str, s;
		for (int i = 0; i < len ; ++i)
		{
			s.Format(TEXT("%02X"), pdata[i]);
			str += s;
		}
		pdata += len;
		length -= len;
		SetDlgItemText(IDC_EDIT2, str);
		s.Format(TEXT("数据长度: %d"), length);
		SetDlgItemText(IDC_STATIC_LEN, s);
		s.Format(TEXT("用时: %d ms"), t);
		SetDlgItemText(IDC_STATIC_TIME, s);
	}
}
